/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.aries.ui.widget.demo.base;

import com.aries.ui.helper.navigation.KeyboardHelper;
import com.aries.ui.helper.navigation.NavigationViewHelper;
import com.aries.ui.view.title.TitleBarView;
import com.aries.ui.widget.demo.ResourceTable;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;

public abstract class BaseAbilitySlice extends AbilitySlice {
    protected TitleBarView titleBar;
    protected ComponentContainer mContentView;
    protected int mType = 0;
    protected NavigationViewHelper mNavigationViewHelper;
    protected KeyboardHelper keyboardHelper;

    protected  abstract  void setTitleBar();

    protected abstract void initView(Intent intent);

    protected boolean isShowLine() {
        return true;
    }

    /**
     * 获取contentView xml id
     *
     * @return 获取contentView xml id
     */
    protected  abstract  int getLayout();

    protected void beforeControlNavigation(NavigationViewHelper navigationHelper) {
    }


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        DirectionalLayout layout = new DirectionalLayout(getContext());
        int par = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        layout.setLayoutConfig(new DirectionalLayout.LayoutConfig(par, par));
        mContentView = (ComponentContainer) LayoutScatter.getInstance(getContext()).parse(getLayout(), null, false);
        layout.addComponent(mContentView);
        setUIContent(layout);
        mContentView.setScrollbarBackgroundColor(new Color(ResourceTable.Color_colorBackground));

        mNavigationViewHelper = NavigationViewHelper.with(getAbility())
                .setLogEnable(false)
                .setNavigationBarLightMode(true);
        mNavigationViewHelper.init();
        beforeControlNavigation(mNavigationViewHelper);
        initTitle();

         keyboardHelper = KeyboardHelper.with(this.getAbility(), ResourceTable.Id_rootResId).setOnKeyboardVisibilityChangedListener(new KeyboardHelper.OnKeyboardVisibilityChangedListener() {
            @Override
            public boolean onKeyboardVisibilityChanged(Ability activity, boolean isOpen, int heightDiff, int navigationHeight) {
                return false;
            }
        });
         this.initView(intent);
    }

    protected void initTitle() {
        titleBar = (TitleBarView)findComponentById(ResourceTable.Id_titleBar);
        if (titleBar == null) {
            return;
        }
        mType = titleBar.getStatusBarModeType();
        //无法设置白底黑字
        if (mType <= 0) {
            //5.0 半透明模式alpha-102
            titleBar.setStatusAlpha(60);
        }
//        titleBar.setTitleMainText(getTitle());
        titleBar.setOnLeftTextClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                onBackPressed();
                terminate();
            }
        });
        setTitleLine(isShowLine());
        setTitleBar();
    }

    public void setTitleLine(boolean enable) {
        titleBar.setDividerVisible(enable);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
